package com.cd.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CheckLogin
{
	private DBUtil database;
	private PreparedStatement psm;
	private ResultSet rs;
	private int check = 1;
	private ReturnResult returnResult;
	private StringBuffer result = new StringBuffer("");

	public ReturnResult checkLogin(String username, String password)
	{
		returnResult = new ReturnResult();
		database = new DBUtil();
		Connection con = database.getConnection();
		try
		{
			psm = con
					.prepareStatement("SELECT username,password from user where username = ?");
			psm.setString(1, username);
			rs = psm.executeQuery();
			if (!rs.next())
			{
				result.append("用户名或密码错误");
				check = 0;
			} else if (!rs.getString(2).equals(password))
			{
				result.append("登录密码错误");
				check = 0;

			} else
			{
				check = 1;
			}

			if (psm.isClosed() == false)
			{
				psm.close();
			}
			if (rs.isClosed() == false)
			{
				rs.close();
			}
			database.closeService();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}

		returnResult.setCheck(check);
		returnResult.setStr(result.toString());
		return returnResult;

	}
}
